package linkedlist;

import java.util.Stack;

/**
 * @author huangxin
 * @date 2019/6/20-9:47
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");

        DoubleLinkedList linkedList = new DoubleLinkedList();
        linkedList.addByOrder(hero4);
        linkedList.addByOrder(hero1);
        linkedList.addByOrder(hero3);
        linkedList.addByOrder(hero2);

        linkedList.list();

        HeroNode2 h = new HeroNode2(2, "卢俊义", "狗狗狗");
        linkedList.update(h);
        System.out.println("更新后");

        linkedList.list();


        linkedList.delete(2);
        System.out.println("删除后");
        linkedList.list();
//
//        //获取倒数第2个节点
//        HeroNode eleByIndex = findLastIndexNode(linkedList.getHead(), 2);
//        System.out.println("倒数第二个");
//        System.out.println(eleByIndex);
//
//        //倒叙打印
//        System.out.println("逆序打印,没有改变链表结构");
//        reversePrint(linkedList.getHead());
//
//
////        System.out.println("有效节点个数为:" + getLength(linkedList.getHead()));
////
////        //遍历
////        linkedList.list();
////
////        //修改节点
////        HeroNode newHeroNode = new HeroNode(2, "小卢", "麒麟");
////        linkedList.update(newHeroNode);
////        linkedList.list();
////
////        //删除节点
////        linkedList.delete(1);
////        linkedList.delete(2);
////        linkedList.delete(2);
////        System.out.println("有效节点个数为:" + getLength(linkedList.getHead()));
////        linkedList.list();
//
////        System.out.println("反转后");
////        reverse(linkedList.getHead());
//
//        HeroNode hero5 = new HeroNode(2, "林冲", "豹子头");
//        HeroNode hero6 = new HeroNode(6, "宋江", "及时雨");
//        HeroNode hero7 = new HeroNode(7, "吴用", "智多星");
//        HeroNode hero8 = new HeroNode(8, "卢俊义", "玉麒麟");
//        SingleLinkedList linkedList2 = new SingleLinkedList();
//        linkedList2.addByOrder(hero5);
//        linkedList2.addByOrder(hero6);
//        linkedList2.addByOrder(hero7);
//        linkedList2.addByOrder(hero8);
//
//
//        System.out.println("合并后");
//        HeroNode merge = merge(linkedList.getHead(), linkedList2.getHead());
//        print(merge);
    }


}

//单向列表
class DoubleLinkedList {
    public HeroNode2 getHead() {
        return head;
    }
    //初始化头节点,头节点不能动,不存放具体的数据
    private HeroNode2 head = new HeroNode2(0, "", "");

    //添加节点到双向列表
    //不考虑编号顺序时
    //1.找到当前链表的最后节点
    //2.将最后节点的next 指向新节点
    public void add(HeroNode2 heroNode) {
        //head节点不能动
        HeroNode2 temp = head;
        //遍历链表,找到最后
        while (true) {
            //找到链表的最后节点
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //当退出循环时,temp就指向了链表的最后
        //双向
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    //排序插入
    public void addByOrder(HeroNode2 heroNode) {
        HeroNode2 temp = head;
        boolean flag = false;
        while (true) {
            //说明temp已经在链表的最后
            if (temp.next == null) {
                break;
            }
            if (temp.next.no > heroNode.no) { //位置找到,就在temp的后面插入
                break;
            } else if (temp.next.no == heroNode.no) {
                flag = true;
                break;
            }
            //后移
            temp = temp.next;
        }
        //判断是否要添加
        if (flag) {
            //编号存在,不能添加
            System.out.printf("准备插入的元素编号%d已经存在,不能加入\n", heroNode.no);
        } else {
            //插入到链表中
            if (temp.next != null) {//判断是否为最后一个节点
                heroNode.next = temp.next;
                temp.next.pre = heroNode;
            }
            temp.next = heroNode;
            heroNode.pre = temp;
        }
    }

    public void update(HeroNode2 heroNode) {
        if (heroNode.no == 0) {
            System.out.println("不能修改根节点");
            return;
        }
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false; //表示是否找到该节点
        //找到需要修改的节点,根据编号
        while (true) {
            if (temp.no == heroNode.no) {
                //找到
                flag = true;
                break;
            }
            temp = temp.next;

            if (temp == null) {
                break;
            }
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
            System.out.println("更新成功");
        } else {
            System.out.printf("没有找到编号:%d 的节点,不能修改\n", heroNode.no);
        }
    }


    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            //找到需要删除的节点
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
            if (temp == null) {
                break;
            }
        }
        if (flag) {
            if (temp.next!=null){
                temp.next.pre = temp.pre;
            }
            temp.pre.next = temp.next;
            System.out.println("删除成功");
        } else {
            System.out.printf("没有找到编号:%d 的节点,不能删除\n", no);
        }
    }


    //遍历链表
    public void list() {
        //如果链表为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //头节点不能动
        HeroNode2 temp = head.next;
        while (true) {
            //判断是否到最后
            if (temp == null) {
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //temp后移
            temp = temp.next;
        }
    }
}

class HeroNode2 {
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 next; // 下一个节点
    public HeroNode2 pre; // 上一个节点


    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
